﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IrrKlang;
using PacManLib;

namespace PacManBoardControl
{
    /// <summary>
    /// Plays a sound.
    /// </summary><remarks>
    /// When you create one of these objects, you specify 
    /// </remarks>
    public class Sound
    {
        #region Public types

        /// <summary>
        /// The sound sample to play.
        /// </summary>
        public enum Sample
        {
            Intro,
            GhostsScared,
            GhostEaten,
            PacManEaten,
            PelletEaten1,
            PelletEaten2,
            Siren
        }

        #endregion

        #region Public methods and properties

        /// <summary>
        /// Plays the sound once.
        /// </summary>
        public void play()
        {
            try
            {
                stop();
                m_sound = m_soundEngine.Play2D(m_soundSource, false, false, false);
            }
            catch (Exception ex)
            {
                Log.log("play: " + ex.Message);
            }
        }

        /// <summary>
        /// Plays the sound repeatedly.
        /// </summary>
        public void playLooped()
        {
            try
            {
                stop();
                m_sound = m_soundEngine.Play2D(m_soundSource, true, false, false);
            }
            catch (Exception ex)
            {
                Log.log("playLooped: " + ex.Message);
            }
        }

        /// <summary>
        /// Stops the sound if it is playing.
        /// If it is restarted, it will start again from the beginning.
        /// </summary>
        public void stop()
        {
            try
            {
                if (m_sound != null)
                {
                    m_sound.Stop();
                    m_sound = null;
                }
            }
            catch (Exception ex)
            {
                Log.log("stop: " + ex.Message);
            }
        }

        #endregion

        #region Internal methods

        /// <summary>
        /// The constructor is internal, as external code
        /// creates them through the PacManBoard.createSound() method.
        /// </summary>
        internal Sound(Sample sample)
        {
            try
            {
                // We create the sound-engine if it hasn't been
                // created yet...
                if(m_soundEngine == null)
                {
                    m_soundEngine = new ISoundEngine();
                }

                // We load the sound...
                string fileName = getResourceName(sample);
                m_soundSource = m_soundEngine.AddSoundSourceFromFile(fileName);
            }
            catch (Exception ex)
            {
                Log.log("Sound constructor: " + ex.Message);
            }
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Converts the sample enum to the resource name.
        /// </summary>
        private string getResourceName(Sample sample)
        {
            switch (sample)
            {
                case Sample.GhostEaten:
                    return "Sounds/GhostEaten.wav";

                case Sample.GhostsScared:
                    return "Sounds/GhostsScared.wav";

                case Sample.Intro:
                    return "Sounds/Intro.wav";

                case Sample.PacManEaten:
                    return "Sounds/PacManEaten.wav";

                case Sample.PelletEaten1:
                    return "Sounds/PelletEaten1.wav";

                case Sample.PelletEaten2:
                    return "Sounds/PelletEaten2.wav";

                case Sample.Siren:
                    return "Sounds/Siren.wav";

                default:
                    return "";
            }
        }

        #endregion

        #region Private data

        // IrrKlang stuff...
        private static ISoundEngine m_soundEngine = null;
        private ISoundSource m_soundSource = null;
        private ISound m_sound = null;

        #endregion

    }
}
